import {BrowserRouter as Router, Switch, Route} from 'react-router-dom';
import './App.css';
import routers from './router'
function DynamicRoute(routes, path = null) {
    let r = [];
    routes.forEach(e => {
        if (path) {
            e.path = path + e.path;
        }
        if (e.children) {
            let cr = DynamicRoute(e.children, e.path);
            // delete e.children;
            r = r.concat(cr)
        }
        r.push({...e});
    })
    return r;
}
const dRoutes = DynamicRoute(routers);
function App() {
    return (
        <Router>
            <Switch>
                {
                    dRoutes.map((route, index) => {
                        return (
                            <Route
                                key={index}
                                path={route.path}
                                exact={route.exact}
                                render={(props) => {
                                    if (route.layout) {
                                        return <route.layout
                                            history={props.history}
                                            Component={route.component}/>
                                    }
                                    return (<route.component/>)
                                }}
                            />
                        )
                    })
                }
            </Switch>
        </Router>
    );
}

export default App;
